/**
 * Copyright © 2014 TingerSure. All rights reserved.
 */
package org.sure.scomet.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.sure.scomet.link.Connection;
import org.sure.scomet.link.ConnectionCenter;
import org.sure.scomet.util.log.LogManager;
import org.sure.scomet.util.log.LogType;

/**
 * The action which request to a connection to Server and return back with a
 * page id.
 * 
 * @author TingerSure
 * 
 */
public class RequestAction {
	/**
	 * The request action.<br>
	 * It will be called when the link request come.
	 * 
	 * @param req
	 *            the request
	 * @param res
	 *            the response
	 */
	public void Action(HttpServletRequest req, HttpServletResponse res) {
		Connection conn = new Connection();
		int pageId = ConnectionCenter.addConnection(conn);
		try {
			PrintWriter write = res.getWriter();
			write.write("r:" + pageId);
		} catch (IOException e) {
			LogManager.logException(
					"An error occurred while connecting created!", e,
					LogType.error);
		}
		ConnectionCenter.getCallBack().linked(pageId, req.getSession());
	}

}
